

import java.io.Serializable;

public class Compte implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String nom;
	private String mdp;
	private int balance;
	
	public Compte(String nom, String password) {
		this.nom = nom;
		this.mdp = password;
		this.balance = 0;
	}

	public boolean verifie(String nom, String password) {
		return this.nom.equals(nom)&&this.mdp.equals(password);
	}

	public int getSolde() {
		return balance;
	}

	public String getNomUtilisateur() {
		return nom;
	}

	public void deposerSomme(int somme) throws Exception{
		if(somme <= 0)
			throw new Exception("la somme à deposr doit > 0");
		this.balance += somme;
	}

	public int retirerSomme(int somme) throws Exception{
		if(somme <= 0 )
			throw new Exception("la somme à retirer doit > 0");
		if(balance < somme)
			throw new Exception("la solde du compte est insuffisante");
		this.balance -= somme;
		return getSolde();
	}

	@Override
	public String toString() {
		return "Compte [nom=" + nom + ", balance=" + balance + "]";
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Compte other = (Compte) obj;
		if (balance != other.balance)
			return false;
		if (mdp == null) {
			if (other.mdp != null)
				return false;
		} else if (!mdp.equals(other.mdp))
			return false;
		if (nom == null) {
			if (other.nom != null)
				return false;
		} else if (!nom.equals(other.nom))
			return false;
		return true;
	}
	
}
